package com.yy.job.spring;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class YYApplicationContextWrapper implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext;

	public static <T> T service(Class<T> clz) {
		return (T) context().getBean(clz);
	}

	public static <T> T bean(Class<T> beanClz, String name) {
		return (T) context().getBean(name, beanClz);
	}

	public static <T> T bean(Class<T> beanClz) {
		return (T) context().getBean(beanClz);
	}

	public static <T> Map<String, T> beans(Class<T> beanClz) {
		return context().getBeansOfType(beanClz);
	}

	public static ApplicationContext context() {
		return applicationContext;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		YYApplicationContextWrapper.applicationContext = applicationContext;
	}
}
